<?php
/**
*
* @package phpBB3
* @version $Id$
* @copyright (c) 2010
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/	
    	
/*
*	This service will:
*	1. If it's in trading time, check all delegations and get related stock price/volume 
*		to see if the delegations can be done.
*	2. If it's not in trading time, get stock price/volume for all corp for each trading day. 
* 
*	Note: this file should be in ASCII format!
*/	
 
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include_once($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'sv_common.' . $phpEx);
// We include config.php here to include the dbpassword again, as some tasks, like fill_corp_info, take much longer time than expected and we may need to re-connect SQL server.
include_once($phpbb_root_path . 'config.' . $phpEx);

//remove time limit.
if (!get_cfg_var('safe_mode')) 
{
	set_time_limit(0);	
} 

// check the service lock.
$a = new service_lock(basename(__FILE__));
if ($a->lock() == false)
{
	// another instance is running.
	echo "\n\nanother instance is running.\n";
	echo date('Y/m/d H:i:s', get_taiwan_time()) . PHP_EOL . PHP_EOL;
	die;	
}

// get taiwan time.		
$time = get_taiwan_time();

// print service log.
print_service_log(__FILE__);
		
if (is_trading_time($time) && is_trading_day($time))
{		
	echo "In trading time, exit\n";
}
else
{	
	echo "not trading time\n";
		
	if (service_task_completed_for_latest_trading_date('get_all_corp_info') == false)
	{
		// check if there is any new corp.
		begin_service_task('get_all_corp_info');
		$insert_directly = true;
		get_all_corp_info($insert_directly);	
		end_service_task('get_all_corp_info');
	}
	
	// 
	// If the equity change event date is X, the equity change should only occur to those before X, not including X. 
	// Take a corp dispatching dividend on August 6 (Tue) as an example, only thos who buy shares before and on August 5 (MON) can have dividends and those who buy shares on August 6 (TUE) cannot have dividend. 
	// 
	// Important assumption: this service should be executed after the midnight of a trading day. In the above example, this service should be executed at 1:00 AM on August 6 (Tue). 
	//
	if (service_task_completed_for_latest_trading_date('handle_equity_change_event', false) == false)
	{
		begin_service_task('handle_equity_change_event');
		handle_equity_change_event();
		end_service_task('handle_equity_change_event');
	}
	
	if (service_task_completed_for_latest_trading_date('update_all_stock_price') == false)
	{
		// get all the stock price/volume info of the latest finished trading day
		begin_service_task('update_all_stock_price');
		update_all_stock_price();
		end_service_task('update_all_stock_price');
	}
	
	if (service_task_completed_for_latest_trading_date('update_all_user_value') == false)
	{
		// update all users total value (including money and holding stocks)
		begin_service_task('update_all_user_value');
		update_all_user_value();
		end_service_task('update_all_user_value');
	}
			
	if (service_task_completed_for_latest_trading_date('get_taiex') == false)
	{
		begin_service_task('get_taiex');
		get_taiex(get_date_from_time($time), true);
		end_service_task('get_taiex');
	}

	if (service_task_completed_for_latest_trading_date('get_capital_reduction_info') == false)
	{
		begin_service_task('get_capital_reduction_info');
		get_capital_reduction_info();
		end_service_task('get_capital_reduction_info');
	}

	if (service_task_completed_for_latest_trading_date('get_dividend_from_otc') == false)
	{
		begin_service_task('get_dividend_from_otc');
		get_dividend_from_otc();
		end_service_task('get_dividend_from_otc');
	}
	
	if (service_task_completed_for_latest_trading_date('get_dividend_from_twse') == false)
	{
		begin_service_task('get_dividend_from_twse');
		get_dividend_from_twse();
		end_service_task('get_dividend_from_twse');
	}
	
	if (service_task_completed_for_latest_trading_date('update_short_sale_suspension_schedule') == false)
	{
		begin_service_task('update_short_sale_suspension_schedule');
		update_short_sale_suspension_schedule();
		end_service_task('update_short_sale_suspension_schedule');
	}

	// calculate visiting user number everyday, including non-trading day
	if (service_task_completed_for_latest_trading_date('cal_visiting_user_num', false) == false)
	{
		// Calculate and record the number of visiting daily, weekly and monthly users 
		begin_service_task('cal_visiting_user_num');
		cal_visiting_user_num();
		end_service_task('cal_visiting_user_num');
	}	
	
	if (service_task_completed_for_latest_trading_date('update_all_stock_moving_average_price') == false)
	{
		begin_service_task('update_all_stock_moving_average_price');
		update_all_stock_moving_average_price();
		end_service_task('update_all_stock_moving_average_price');
	}		
		
}

// print service log end.
print_service_log_end();
?>